﻿using ClinicSystem.configure;
using Org.BouncyCastle.Utilities;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace ClinicSystem.FormPage
{
    public partial class WndNewMedical : Form
    {
        private PrintDocument printDocument = new PrintDocument();
        private PrintPreviewDialog printPreviewDialog = new PrintPreviewDialog();
        private PrintDialog printDialog = new PrintDialog();
        private PageSetupDialog pageSetupDialog = new PageSetupDialog();

        public string gender = "";
        internal int number;

        public WndNewMedical()
        {
            InitializeComponent();
            TopLevel = false;
            number = Convert.ToInt32(textBox_number.Text);

            Timer timer = new Timer();
            timer.Interval = 1000; // 每 1000 毫秒（1 秒）更新一次
            timer.Tick += (sender, e) =>
            {
                label17.Text = DateTime.Now.ToString("yyyy年MM月dd日");
            };
            timer.Start();


            dataGridView_List.CellValueChanged += (s, e) =>
            {
                if (e.ColumnIndex == 1)
                {
                    DataGridViewCell cell = dataGridView_List.Rows[e.RowIndex].Cells[e.ColumnIndex];
                    if (cell.Value != null)
                    {
                        dataGridView_List.Rows[e.RowIndex].Cells[e.ColumnIndex+1].Value =
                        Convert.ToInt32(dataGridView_List.Rows[e.RowIndex].Cells[e.ColumnIndex].Value) * number;
                    }
                }
            };
            textBox_number.TextChanged += (s, e) =>
            {
                if (Regex.IsMatch(textBox_number.Text, @"^[0-9]*$")&& !string.IsNullOrEmpty(textBox_number.Text))
                {
                    number = Convert.ToInt32(textBox_number.Text);
                    foreach (DataGridViewRow Row in dataGridView_List.Rows)
                    {
                        if (!Row.IsNewRow)
                        {
                            Row.Cells[2].Value = number * Convert.ToInt32(Row.Cells[1].Value);
                        }
                    }
                }
            };
            genderChoose1.ChoiceChanged += (sender, e) =>
            {
                gender = e.value;
            };
			// 设置打印预览对话框
			printPreviewDialog.Document = printDocument;
            printDialog.Document = printDocument;
            pageSetupDialog.PageSettings=printDocument.DefaultPageSettings;
            pageSetupDialog.PrinterSettings = printDocument.PrinterSettings;
			printDocument.PrintPage += PrintDocument_PrintPage;

            label7.Visible = false;
            textBox_medicalID.Visible = false;
        }

        private void PrintDocument_PrintPage(object sender, PrintPageEventArgs e)
        {
            float width = e.Graphics.VisibleClipBounds.Width;
            float height = e.Graphics.VisibleClipBounds.Height;
            //打印内容
            string printName="姓名："+ComboBox_name.Text;
            string tabelName = "松隐堂中医诊所代煎单";
            Font printFont1 = new Font("楷体", 20);
            e.Graphics.DrawString(tabelName, printFont1, Brushes.Black, 500, 40);
            e.Graphics.DrawLine(Pens.Black, 8, 65, 1000, 65);
            e.Graphics.DrawString(printName, printFont1, Brushes.Black, 10, 70);
            printFont1.Dispose();
            //if (controltoprint != null)
            //{
            //    // 获取控件的截图
            //    Bitmap bitmap1 = new Bitmap(controltoprint.Width, controltoprint.Height);
            //    controltoprint.DrawToBitmap(bitmap1, controltoprint.ClientRectangle);
            //    // 将控件内容绘制到打印页面
            //    e.Graphics.DrawImage(bitmap1, 0, 0);
            //}


        }
        internal printSet printSet;
        private void button_print_Click(object sender, EventArgs e)
        {
            if(values.Count<1)
            {
                MessageBox.Show("请先保存", "提示");
                return;
            }
            printSet = new printSet();
            printSet.Show();
           
        }
        //internal PersonInformation newPatient = new PersonInformation();
        internal List<string> values = new List<string>();
        
        private void button_MedicalSave_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(ComboBox_name.Text.Trim()) || !string.IsNullOrEmpty(textBox_age.Text.Trim())
                || !string.IsNullOrEmpty(textBox_age.Text.Trim()))
            //
            {

                
                values.Add("'" + ComboBox_name.Text + "'");//姓名
                values.Add("'" + gender + "'");//性别
                values.Add(textBox_age.Text);//年龄
                values.Add("'" + DateTime.Now.ToString("yyyyMMdd") + "'");//病历号
                values.Add("'" + textBox_telNumber.Text + "'");//电话
                values.Add("'" + textBox_address.Text + "'");//地址
                values.Add("DATE('now')");//日期
                values.Add("'" + textBox_mainsuit.Text + "'");//主诉
                values.Add("'" + textBox_history.Text + "'");//既往史
                values.Add("'" + textBox_current.Text + "'");//现病史
                values.Add("'" + textBox_pulse.Text + "'");//脉象
                values.Add("'" + textBox_tongue.Text + "'");//舌象
                values.Add("'" + textBox_diagnoseResult.Text + "'");//诊断结果
                List<string> PeopleColname = new List<string> { "name","gender","age","medicalID","telnumber",
                    "address","date","mainSuit","history","current","pulse","tongue","diagnoseResult"};
                Load_OutFile.InsertData("person", PeopleColname, values);
                GlobalVar.temp = values;
            }
            else
            {
                MessageBox.Show("请完善基本信息");
                return;
            }

            List<string> name = new List<string>();

            List<string> MedicalColname = new List<string> { "peopleName", "drugName", "dosage", "note", "date","number" };

            foreach (DataGridViewRow Row in dataGridView_List.Rows)
            {
                if (!Row.IsNewRow)
                {

                    List<string> value = new List<string>();
                    value.Add("'" + ComboBox_name.Text + "'");

                    string tempname = Row.Cells[0].Value?.ToString();
                    value.Add("'" + tempname + "'");
                    string tempvalue = Row.Cells[1].Value?.ToString();
                    value.Add(tempvalue);
                    string tempnote = Row.Cells[2].Value?.ToString();
                    value.Add("'" + tempnote + "'");
                    value.Add("DATE('now')");
                    value.Add("'" + textBox_number.Text + "'");
                    Load_OutFile.InsertData("medicineList", MedicalColname, value);
                }
            }
        }

        private void ComboBox_name_TextChanged(object sender, EventArgs e)
        {
            string checkname = ComboBox_name.Text;
            List<string> namelist = Load_OutFile.QueryData("person", "name", "name LIKE  " + "'" + checkname + "%'");

            foreach (string names in namelist)
            {
                ComboBox_name.Items.Add(names);
            }
            //if (ComboBox_name.Items.Count != namelist.Count)
            //{
            //    ComboBox_name.Items.Clear();

            //}
        }
    }
}
